/**
 * 
 */
package de.cbf.cam.dsa.facet.sf.edit;

import static de.cbf.util.ui.test_support.ConstantsSWTBOTUtil.DEFAULT_SWT_BOT_KEY;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;

import de.cbf.cam.dsa.facet.sf.helper.PrerequisiteSfReference;
import de.cbf.cam.dsa.facet.sf.test_suppport.ConstantsSWTBOTSf;
import de.cbf.util.entity.facet.edit.composites.AbstractCompositeEditFacet;
import de.cbf.util.ui.composites.CompositeTableEdit;
import de.cbf.util.ui.listener.UiChangeEvent;

/**
 * @author Nebelritter
 *
 */
public class CompositeEditSf extends AbstractCompositeEditFacet {
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_BE_SOURCES				= 100;
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_SPECIALIZATIONS			=  -1;
	/** min: , default: ,max:  */
	public static final int HEIGHT_HINT_COMPENSATION_TALENTS	=  -1;
	
	public static final int EVT_EDIT_TALENT_TYPE			= 10;
	

	public static final String TEXT_LABEL_SF_TYPE			= "Sf type: ";
	public static final String TEXT_LABEL_PREREQUISITE_SF	= "Prerequisite Sf: ";
	
			
	protected Combo cb_SfType;		
	protected CompositeTableEdit<PrerequisiteSfReference> cp_PrereqSf;	
	
	public CompositeEditSf(Composite parent, int style, FormToolkit toolkit) {
		super(parent, style, toolkit);		
	}

	@Override
	protected GridLayout createLayout() {
		return new GridLayout(2, false);
	}
		
	@Override
	protected void createContent(Composite compositeThis) {
		
		//fSfType
		Label lb_TalentType = toolkit.createLabel(compositeThis, TEXT_LABEL_SF_TYPE,SWT.NONE);
		lb_TalentType.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));

		cb_SfType = new Combo(compositeThis,SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		toolkit.adapt(cb_SfType);
		cb_SfType.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
		
		cb_SfType.setData(DEFAULT_SWT_BOT_KEY,ConstantsSWTBOTSf.FACET_Sf_COMBO_SF_TYPE_ID);		
		cb_SfType.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UiChangeEvent event = new UiChangeEvent(CompositeEditSf.this);
				event.setNewValue(cb_SfType.getText());
				event.setIndex(EVT_EDIT_TALENT_TYPE);
				fireUIChangeEvent(event);
			}
		});	
				
		//prerequisite Sf
		cp_PrereqSf = createTableComposite_PrereqSf(TEXT_LABEL_PREREQUISITE_SF, compositeThis);
		GridData gd_CompensationTalents = new GridData(SWT.FILL,SWT.FILL,true,true,2,1);
		gd_CompensationTalents.heightHint = HEIGHT_HINT_COMPENSATION_TALENTS;
		cp_PrereqSf.setLayoutData(gd_CompensationTalents);
		cp_PrereqSf.setData(DEFAULT_SWT_BOT_KEY, ConstantsSWTBOTSf.FACET_Sf_COMPOSITE_PREREQUISITE_SF_EDIT_ID);
	}
	
	
	protected CompositeTableEdit<PrerequisiteSfReference> createTableComposite_PrereqSf(
			String textCompensationTalents, Composite parent) {
		CompositeTableEdit<PrerequisiteSfReference> composite =
				CompositeTableEdit.createComposite(PrerequisiteSfReference.class, parent, SWT.DOUBLE_BUFFERED);
		composite.setHeaderText(textCompensationTalents);
		return composite;
	}

	public CompositeTableEdit<PrerequisiteSfReference> getCompositePrereqSf(){
		return cp_PrereqSf;				
	}
	
	
	public void setPossibleSfTypes(String[] sfTypes){
		String selected = cb_SfType.getText();
		cb_SfType.setItems(sfTypes);
		int selectionIndex = cb_SfType.indexOf(selected);
		cb_SfType.select(selectionIndex);
	}
	
	public String getSfType(){
		return cb_SfType.getText();
	}
	
	public void setSfType(String value) {
		if(value == null){
			cb_SfType.deselectAll();
			return;
		}
		if( ! cb_SfType.getText().equals(value)){
			cb_SfType.setText(value);
		}
	}
}
